﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using QLKCB.Model;

namespace QLKCB.Common
{
    class Utils
    {

        ConnectDB connectDB = new ConnectDB();

        public string genKey(string sql, object[] param, int numOfPre, string prefixKey)
        {
            string strKey = "";
            char[] sep = { '-' };
            DataTable dtKey = connectDB.ExeQuery(sql, param);
            DateTime today = DateTime.Today;
            if (dtKey.Rows.Count > 0)
            {
                string tempKey = dtKey.Rows[0][0].ToString();
                string tempKey1 = tempKey.Split(sep)[0];
                string tempKey2 = tempKey.Split(sep)[1];
                int stt = int.Parse(tempKey2);
                int sizeCode = stt.ToString().Length;

                string typeKey = tempKey1.Substring(0, numOfPre);
                string nowDate = today.ToString("yyyyMMdd");
                string genCode = "";

                if ((typeKey + nowDate).Equals(tempKey1))
                {
                    for (int i = 6; i >= 0; i--)
                    {
                        if (i == sizeCode)
                        {
                            break;
                        }
                        genCode += "0";
                    }
                    genCode += (stt + 1).ToString();
                }
                else
                {
                    genCode = "000001";
                }

                strKey = typeKey + nowDate + "-" + genCode;
            }
            else
            {
                string nowDate = today.ToString("yyyyMMdd");
                strKey = prefixKey + nowDate + "-000001";
            }

            return strKey;

        }

        public Hashtable getDoiTuong()
        {
            Hashtable mapDoiTuong = new Hashtable();
            mapDoiTuong.Add("BHYT", "Bảo hiểm y tế");
            mapDoiTuong.Add("THUPHI", "Thu phí");
            mapDoiTuong.Add("MIENPHI", "Miễn phí");
            return mapDoiTuong;
        }

    }
}
